﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Thor.DBUtility;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;

namespace ThorActorWeb.Controllers
{
    public class HomeController : AsyncController
    {


        public class ThorRedisTestClass
        {
            public string a { get; set; }
            public int b { get; set; }

            public bool c { get; set; }

            public ThorRedisTestClass d { get; set; }
            public ThorRedisTestClass()
            {
                this.a = Guid.NewGuid().ToString();
                this.b = new Random(DateTime.Now.Millisecond).Next(10, 100);
                this.c = true;
                this.d = null;
            }
            public ThorRedisTestClass(bool isEmpty, string a = "a1", int b = 2, bool c = false)
            {
                if (isEmpty)
                {
                    this.a = a;
                    this.b = b;
                    this.c = c;
                    this.d = new ThorRedisTestClass();
                }
                else
                {
                    this.a = Guid.NewGuid().ToString();
                    this.b = new Random(DateTime.Now.Millisecond).Next(10, 100);
                    this.c = true;
                    this.d = null;
                }
            }
        }
        private static Thor.Common.LogUtility log = new Thor.Common.LogUtility();

        public async Task<ActionResult> Index()
        {
            



            //Parallel.For(0, 100, i => { log.SaveLog(Guid.NewGuid().ToString(), @"E:\Users\sfissw\Desktop\ThorActorWeb\ThorActorWeb\1.txt"); });
            //return View();
            ////初始化字符串 就说  ip1:端口1,ip2:端口2,ip3:端口3,...,allowAdmin=true
            ////节点从0开始   第一个是0号节点   默认也是第一个，集群只用第一个即可
            ////////////////RedisUtility redis = RedisUtility.GetInstance("10.133.8.81:6379,10.133.8.81:6379,allowAdmin=true,abortConnect=false");
            ////////////////Parallel.For(0, 1000, i =>
            ////////////////{
            ////////////////    for (int j = 0; j < 100; j++)
            ////////////////    {
            ////////////////        string key = System.Guid.NewGuid().ToString("N");
            ////////////////        StringBuilder sb = new StringBuilder();
            ////////////////        for (int k = 0; k < 100 / 4; k++)
            ////////////////        {
            ////////////////            sb.Append(key);
            ////////////////        }
            ////////////////        bool value =redis.StringSet(key, sb.ToString());
            ////////////////        string key2 = redis.StringGet(key);
            ////////////////        if (key2 == null || key2.Length < 1)
            ////////////////        {
            ////////////////            //for (int k = 0; k < 10; k++)
            ////////////////            //{
            ////////////////            //    System.Threading.Thread.Sleep(1000);
            ////////////////            //    key2 = redis.StringGet(key);
            ////////////////            //    if (key2 != null && key2.Length > 1)
            ////////////////            //    {
            ////////////////            //        break;
            ////////////////            //    }
            ////////////////            //}
            ////////////////            //if (key2 != null && key2.Length > 1)
            ////////////////            //{
            ////////////////            //    log.SaveLog(key + "_" + DateTime.Now, @"C:\Users\Administrator\Desktop\ThorActorWeb\ThorActorWeb\log.txt");
            ////////////////            //}
            ////////////////            //else
            ////////////////            //{
            ////////////////                log.SaveLog(key + "_" + DateTime.Now, @"C:\Users\Administrator\Desktop\ThorActorWeb\ThorActorWeb\log2.txt");
            ////////////////            //}

            ////////////////        }
            ////////////////        else
            ////////////////        {
            ////////////////            log.SaveLog(key + "_" + DateTime.Now, @"C:\Users\Administrator\Desktop\ThorActorWeb\ThorActorWeb\log.txt");
            ////////////////        }
            ////////////////    }
            ////////////////    return;
            ////////////////});
            //var r = new System.Random(System.DateTime.Now.Millisecond);
            //string key = Guid.NewGuid().ToString();
            //StringBuilder response = new StringBuilder();

            ////测试HashSet<T>和HashGet<T> HashSetAll<T>和HashGetAll<T>，但是批量操作都不建议使用
            ////List<ThorRedisTestClass> list1 = new List<ThorRedisTestClass>();
            ////for (int i = 0; i < 10; i++)
            ////{
            ////    list1.Add(new ThorRedisTestClass(true));
            ////}
            ////await redis.HashSet<ThorRedisTestClass>(key, list1[0]);
            ////response.Append("<br />");
            ////await redis.HashSet<int>(key, "b", 4535353);
            ////var tt = await redis.HashGet<int>(key, "b");
            //////用类型的属性字段来枚举出这个属性字段的名字字符串name
            ////// MessageBox.Show(((DefaultValueAttribute)(AA.GetType().GetProperty(AA.GetNeededPropName).GetCustomAttributes(typeof(DefaultValueAttribute), false)[0])).Value.ToString()); //1
            ////response.Append((tt));

            ////response.Append((tt));

            ////////ThorRedisTestClass[] values = await redis.DeQueueRange<ThorRedisTestClass>(key, list1.Count());


            ////////foreach (ThorRedisTestClass item in values)
            ////////{
            ////////    response.Append("<br />");
            ////////    response.Append(item.d.a);
            ////////}


            //////测试批量EnQueue<T>和DeQueueRange<T>，但是批量操作都不建议使用
            ////List<ThorRedisTestClass> list1 = new List<ThorRedisTestClass>();
            ////for (int i = 0; i < 10; i++)
            ////{
            ////    list1.Add(new ThorRedisTestClass(true));
            ////}
            ////await redis.EnQueue<ThorRedisTestClass>(key, list1.ToArray());

            ////ThorRedisTestClass[] values = await redis.DeQueueRange<ThorRedisTestClass>(key, list1.Count());


            ////foreach (ThorRedisTestClass item in values)
            ////{
            ////    response.Append("<br />");
            ////    response.Append(item.d.a);
            ////}



            //////测试批量EnQueue<T>和DeQueueRange<T>，但是批量操作都不建议使用
            ////List<string> list1 = new List<string>();
            ////for (int i = 0; i < 10000; i++)
            ////{
            ////    list1.Add("a" + i);
            ////}
            ////await redis.EnQueue<string>(key, list1.ToArray());

            ////string[] values = await redis.DeQueueRange<string>(key, list1.Count());

            ////foreach (string item in values)
            ////{
            ////    response.Append("<br />");
            ////    response.Append(item);
            ////}


            //测试批量Set<T>和Get<T>，但是批量操作都不建议使用，另外Get回来的时候是与key组顺序一致的结果value组
            //IDictionary<string, string> dict = new Dictionary<string, string>();
            //for (int i = 0; i < 2; i++)
            //{
            //    dict.Add("a" + i, new byte[0]);
            //}
            //dict.Clear();
            //System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
            //xml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><Tree><TreeNode Text=\"系统管理\"><TreeNode Text=\"系统配置\"><TreeNode Text=\"菜单管理\" NavigateUrl=\"~/admin/ConstManager.aspx\" /><TreeNode Text=\"系统常量配置\" NavigateUrl=\"~/admin/Base/UserQuery.aspx\" /><TreeNode Text=\"登录用户管理\" NavigateUrl=\"~/admin/Base/MenuQuery.aspx\" /><TreeNode Text=\"角色权限\" NavigateUrl=\"~/admin/Base/MenuRightsQuery.aspx\" /><TreeNode Text=\"页面管理\" NavigateUrl=\"~/admin/Base/PageManager.aspx\" /></TreeNode><TreeNode Text=\"基础信息\"><TreeNode Text=\"网点信息维护\" NavigateUrl=\"~/admin/NetWorkManage.aspx\" /><TreeNode Text=\"员工信息维护\" NavigateUrl=\"~/admin/UsersManage.aspx\" /><TreeNode Text=\"常用地址维护\" NavigateUrl=\"~/admin/AddressManage.aspx\" /></TreeNode></TreeNode></Tree>");
            //if (xml == null)
            //    return null;
            //System.IO.MemoryStream _memory = new System.IO.MemoryStream();
            //BinaryFormatter formatter = new BinaryFormatter();
            //formatter.Serialize(_memory, xml);
            //_memory.Position = 0;
            //byte[] read = new byte[_memory.Length];
            //_memory.Read(read, 0, read.Length);
            //_memory.Close();
            //dict.Add("aaas", "<?xml version=\"1.0\" encoding=\"utf-8\"?><Tree><TreeNode Text=\"系统管理\"><TreeNode Text=\"系统配置\"><TreeNode Text=\"菜单管理\" NavigateUrl=\"~/admin/ConstManager.aspx\" /><TreeNode Text=\"系统常量配置\" NavigateUrl=\"~/admin/Base/UserQuery.aspx\" /><TreeNode Text=\"登录用户管理\" NavigateUrl=\"~/admin/Base/MenuQuery.aspx\" /><TreeNode Text=\"角色权限\" NavigateUrl=\"~/admin/Base/MenuRightsQuery.aspx\" /><TreeNode Text=\"页面管理\" NavigateUrl=\"~/admin/Base/PageManager.aspx\" /></TreeNode><TreeNode Text=\"基础信息\"><TreeNode Text=\"网点信息维护\" NavigateUrl=\"~/admin/NetWorkManage.aspx\" /><TreeNode Text=\"员工信息维护\" NavigateUrl=\"~/admin/UsersManage.aspx\" /><TreeNode Text=\"常用地址维护\" NavigateUrl=\"~/admin/AddressManage.aspx\" /></TreeNode></TreeNode></Tree>");


            //bool value = redis.StringSet(dict);
            //string sss = redis.StringGet("Session_1");
            //sss = sss;
            //string[] keys = dict.Keys.ToArray();
            //dict.Clear();
            ////string[] values = await redis.StringGet(keys);

            ////foreach(string item in values)
            ////{
            ////    response.Append("<br />");
            ////    response.Append(item);
            ////}



            //////测试批量StringSet，但是批量操作都不建议使用
            ////IDictionary<string, string> dict = new Dictionary<string, string>();
            ////for (int i = 0; i < 10000; i++)
            ////{
            ////    dict.Add("a" + i, i + "");
            ////}
            ////dict.Add("7788250", "7788250的植物");
            ////await redis.StringSet(dict);
            ////dict.Clear();
            ////response.Append(await redis.StringGet("7788250"));



            //////redis EnQueue<T> DeQueue<T>压力测试
            ////string br = "<br />";
            ////for (int i = 0; i < 20; i++)
            ////{
            ////    int tmp = r.Next(1000000, 10000000);
            ////    await redis.EnQueue<int>(key, tmp);
            ////    response.Append(i + 1);
            ////    response.Append("_");
            ////    response.Append(tmp);
            ////    response.Append(br);
            ////}
            ////await System.Threading.Tasks.Task.Delay(100000);
            ////for (int i = 0; i < 20; i++)
            ////{
            ////    response.Append(i + 1);
            ////    response.Append("_");
            ////    response.Append(await redis.DeQueue<int>(key));
            ////    response.Append(br);
            ////}


            //////redis Set<T> Get<T>压力测试

            ////await redis.Set<int>(key, r.Next(1000000, 10000000));
            ////Response.Clear();
            ////await redis.Set<Object>(key, new Object());

            ////response.Append(await redis.Get<Object>(key));
            ////key = Guid.NewGuid().ToString();
            ////await redis.Set<int>(key, r.Next(1000000, 10000000));
            ////response.Append(await redis.Get<int>(key));
            ////for (int i = 0; i < 100; i++)
            ////{
            ////    key = Guid.NewGuid().ToString();
            ////    await redis.Set<int>(key, r.Next(1000000, 10000000));
            ////    response.Append(await redis.Get<int>(key));
            ////}
            ////for (int i = 1; i <= 1000; i++)
            ////{
            ////    response.Append(Guid.NewGuid().ToString());
            ////}
            ////key = Guid.NewGuid().ToString();
            ////await redis.Set<int>(key, r.Next(1000000, 10000000));
            ////response.Append(await redis.Get<int>(key));


            //////redis StringSet StringGet压力测试
            ////key = Guid.NewGuid().ToString();
            ////await redis.StringSet(key, Guid.NewGuid().ToString());
            ////Response.Clear();
            ////key = Guid.NewGuid().ToString();
            ////await redis.StringSet(key, Guid.NewGuid().ToString());
            ////response.Append(await redis.StringGet(key));
            ////for (int i = 0; i < 100; i++)
            ////{
            ////    key = Guid.NewGuid().ToString();
            ////    await redis.StringSet(key, Guid.NewGuid().ToString());
            ////    response.Append(await redis.StringGet(key));
            ////}
            ////for (int i = 1; i <= 1000; i++)
            ////{
            ////    response.Append(Guid.NewGuid().ToString());
            ////}
            ////key = Guid.NewGuid().ToString();
            ////await redis.StringSet(key, Guid.NewGuid().ToString());
            ////response.Append(await redis.StringGet(key));
            //return Content(response.ToString());
            return View();
        }

        public async Task<ActionResult> About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public async Task<ActionResult> Contact()
        {
            //ViewBag.Message = "Your contact page.";
            //var db = new ThorEFModel.Models.ThorModel();
            //var r = db.Database.SqlQuery<Thor.ThorEFModel.Models.UserAndLoginlog>("select top 1 * from user;select top 1 * from loginlog");
            return View();
        }

        public async Task<ActionResult> Login()
        {
            ////ThorCache tc = new ThorCache();
            //RedisUtility redis = new RedisUtility("127.0.0.1:6379,127.0.0.1:6379,allowAdmin=true");

            //Dictionary<string, string> dict = new Dictionary<string, string>();
            //dict.Add("c", "3");
            //bool x = await redis.StringSet(dict);
            //var sss = (await redis.StringGet(new string[] { "c" }));
            //var s = await redis.StringGet("b");
            //long l = 0;
            ////long l = await redis.Push<string>("list2", new string[] { "a1", "a2", "a3", "a4" });
            //string key="list14";
            //l = await redis.EnQueue<string>(key, new string[] { "a1", "a2", "a3" });
            ////l = await redis.EnQueue<string>(key, "1");
            ////l = await redis.EnQueue<string>(key, "2");
            ////l = await redis.EnQueue<string>(key, "3");
            ////l = await redis.EnQueue<string>(key, "b2");
            ////l = await redis.EnQueue<string>(key, "c3");
            //string[] v = await redis.DeQueueRange<string>(key,100);
            //long ll = await redis.ListLength(key);
            ////string[] vv = await redis.PopRange<string>("list1",11);
            //ViewBag.Message = l.ToString() + "_" + v[0] + "_" + v[1] + "_" + v[2] + "_" + ll;
            //RedisUtility redis = RedisUtility.GetInstance("127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,allowAdmin=true");
            //string key = Guid.NewGuid().ToString();
            //await redis.StringSet(key, key);
            //Response.Clear();
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //await redis.StringSet(key, key);
            //Response.Write(await redis.StringGet(key));
            //for (int i = 1; i <= 1000; i++)
            //{
            //    Response.Write(Guid.NewGuid().ToString());
            //}
            //await redis.StringSet(key, key);
            //return Content(await redis.StringGet(key));
            return View();
        }
    }
}